package servlet;

import bean.Result;
import com.google.gson.Gson;
import dao.CartDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "addCartServlet", value = "/addCartServlet.s")
public class addCartServlet extends HttpServlet {
    private CartDao dao = new CartDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String uid = request.getParameter("uid");
        final String proid = request.getParameter("proid");
        final String num = request.getParameter("num");
        Result result;
        //根据proid查询购物车，若存在，则提示语句存在，数量加一
        final Map<String, Object> map = dao.queryDataByProid(uid,proid);
        if (map!=null){
            //数量加一
            dao.updateNum(uid,proid);
            result =new Result(1,"该商品已经在购物车,数量加一",null);
        }else{
            final int status = dao.addData(uid, num, proid);
            if (status==1){
                result =new Result(1,"添加成功",null);
            }else {
                result =new Result(0,"添加失败",null);
            }
        }

        final String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }
}
